﻿@inject IPopupService PopupService

<MButton OnClick="ShowBasic">Basic Confirm</MButton>

<MDivider Class="my-4" />

<MRadioGroup @bind-Value="alertType" Row>
    <MRadio Value="AlertTypes.Success" Label="@nameof(AlertTypes.Success)"></MRadio>
    <MRadio Value="AlertTypes.Warning" Label="@nameof(AlertTypes.Warning)"></MRadio>
    <MRadio Value="AlertTypes.Error" Label="@nameof(AlertTypes.Error)"></MRadio>
    <MRadio Value="AlertTypes.Info" Label="@nameof(AlertTypes.Info)"></MRadio>
</MRadioGroup>

<MButton OnClick="ShowTyped">Typed Confirm</MButton>

<MDivider Class="my-4" />

<MCheckbox @bind-Value="throwException" Label="Throw exception"></MCheckbox>
<MButton OnClick="ShowAsync">Async Confirm</MButton>

<MDivider Class="my-4" />

<MButton OnClick="ShowCustom">Custom Confirm</MButton>

@code {

    private AlertTypes alertType = AlertTypes.None;
    private bool throwException;

    private async Task ShowBasic()
    {
        var confirmed = await PopupService.ConfirmAsync("Title", "This is the confirm content!");

        await HandleConfirmResult(confirmed);
    }

    private async Task ShowTyped()
    {
        var confirmed = await PopupService.ConfirmAsync(
            "Title",
            "This is the confirm content!",
            alertType);

        await HandleConfirmResult(confirmed);
    }

    private async Task ShowAsync()
    {
        await PopupService.ConfirmAsync(
            "Title",
            "This is the confirm content!",
            async args =>
            {
                try
                {
                    await Task.Delay(1000);

                    if (throwException)
                    {
                        throw new Exception("something wrong!");
                    }

                    await PopupService.EnqueueSnackbarAsync("Handle onOk successfully!", AlertTypes.Success);
                }
                catch (Exception e)
                {
                    args.Cancel();

                    await PopupService.EnqueueSnackbarAsync(e, true);
                }
            });
    }

    private async Task ShowCustom()
    {
        var confirmed = await PopupService.ConfirmAsync(param =>
        {
            param.Title = "Title";
            param.Content = "This is the confirm content!";
            param.OkText = "Confirm";
            param.CancelText = "Ignore";
        });

        await HandleConfirmResult(confirmed);
    }

    private async Task HandleConfirmResult(bool confirmed)
    {
        if (confirmed)
        {
            await PopupService.EnqueueSnackbarAsync("Ok was clicked!", AlertTypes.Info);
        }
        else
        {
            await PopupService.EnqueueSnackbarAsync("Cancel was clicked!", AlertTypes.Warning);
        }
    }

}
